<?php
class Mbusiness extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	
	function updateBusiness($data) {
		$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
		$this->db->where ( 'id', $data ['id'] );
		$this->db->update ( 'business', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function addBusiness($data) {
		$this->db->insert ( 'business', $data );
		return $this->db->insert_id ();
	}
	
	function getBusinessbyId($id) {
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'business' );
		return $query->row_array ();
	}
	
	function selectAllBusinessforExcel() {
		$this->db->select ( 'name,address,phone,class1,class2,region,area,recycle,details' );
		$query = $this->db->get ( 'business' );
		return $query->result_array ();
	}
	
	function getBranchListbyGroup($name, $id) {
		$this->db->select ( 'business.id,business.name,address,phone,region.name as region' );
		$this->db->where ( 'groupName', $name );
		$this->db->where ( 'region = region.id' );
		$query = $this->db->get ( 'business,region' );
		return $query->result_array ();
	}
	
	function getBusinessListbyWhere($where, $page, $num, $search, $group) {
		if ($group) {
			$this->db->select ( 'id,name,groupName,address,phone,recycle,logoImage,state,details' );
			$this->db->where ( "(state='N' OR state = 'U')" );
			$this->db->where ( 'groupName', $group );
			if ($where)
				$this->db->where ( $where );
			if ($search != '')
				$this->db->where ( $search );
			$this->db->limit ( $num, $page );
			$query = $this->db->get ( 'business' );
			return $query->result_array ();
		} else {
			$this->db->select ( 'groupName,recycle,logoImage,state,count(id) as num,updateTime' );
			$this->db->where ( "(state='N' OR state = 'U')" );
			if ($where)
				$this->db->where ( $where );
			if ($search != '')
				$this->db->where ( $search );
			$this->db->group_by ( "groupName" );
			$this->db->order_by ( "updateTime", "desc" );
			$this->db->limit ( $num, $page );
			$query = $this->db->get ( 'business' );
			return $query->result_array ();
		}
	}
	
	function getClass1List($id, $search, $region, $area, $group) {
		$this->db->select ( 'class1.id as id,class1.name as name,count(business.id) as num' );
		$this->db->where ( 'class1.id = business.class1' );
		$this->db->where ( "(state='N' OR state = 'U')" );
		if ($group)
			$this->db->where ( 'groupName', $group );
		if ($id > 0) {
			$this->db->where ( 'class1.id', $id );
		}
		if ($search != '')
			$this->db->where ( $search );
		if ($region > 0)
			$this->db->where ( 'business.region', $region );
		if ($area > 0)
			$this->db->where ( 'business.area', $area );
		$this->db->group_by ( "business.class1" );
		$this->db->having ( 'num > 0' );
		$this->db->order_by ( "num", "desc" );
		$query = $this->db->get ( 'class1,business' );
		return $query->result_array ();
	}
	
	function getClass2List($id1, $id2, $search, $region, $area, $group) {
		if ($id1 > 0) {
			$this->db->select ( 'class2.id as id,class2.name as name,count(business.id) as num' );
			$this->db->where ( 'class2.id = business.class2' );
			$this->db->where ( "(state='N' OR state = 'U')" );
			$this->db->where ( 'class2.class1', $id1 );
			$this->db->where ( 'business.class1', $id1 );
			if ($group)
				$this->db->where ( 'groupName', $group );
			if ($id1 > 0) {
				$this->db->where ( 'class2.class1', $id1 );
			}
			if ($id2 > 0) {
				$this->db->where ( 'class2.id', $id2 );
			}
			if ($search != '')
				$this->db->where ( $search );
			if ($region > 0)
				$this->db->where ( 'business.region', $region );
			if ($area > 0)
				$this->db->where ( 'business.area', $area );
			
			$this->db->group_by ( "business.class2" );
			$this->db->having ( 'num > 0' );
			$this->db->order_by ( "num", "desc" );
			$query = $this->db->get ( 'class2,business' );
			return $query->result_array ();
		} else
			return NULL;
	}
	
	function getClass1ListAll() {
		$query = $this->db->get ( 'class1' );
		return $query->result_array ();
	}
	
	function getRegionListAll() {
		$query = $this->db->get ( 'region' );
		return $query->result_array ();
	}
	
	function getClass2ListAll($id) {
		if ($id > 0) {
			$this->db->where ( 'class1', $id );
			$query = $this->db->get ( 'class2' );
			return $query->result_array ();
		} else
			return NULL;
	}
	
	function getAreaListAll($id) {
		if ($id > 0) {
			$this->db->where ( 'region', $id );
			$query = $this->db->get ( 'area' );
			return $query->result_array ();
		} else
			return NULL;
	}
	
	function getClass2byId($id) {
		$this->db->select ( 'name,class1' );
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'class2' );
		return $query->row_array ();
	}
	
	function getClass1byId($id) {
		$this->db->select ( 'name' );
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'class1' );
		return $query->row_array ();
	}
	
	function getRegionbyId($id) {
		$this->db->select ( 'name' );
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'region' );
		return $query->row_array ();
	}
	
	function getAreabyId($id) {
		$this->db->select ( 'name,region' );
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'area' );
		return $query->row_array ();
	}
	
	function getRegionList($id, $search, $class1, $class2, $group) {
		$this->db->select ( 'region.id as id,region.name as name,count(region.id) as num' );
		$this->db->where ( 'region.id = business.region' );
		$this->db->where ( "(state='N' OR state = 'U')" );
		if ($id > 0) {
			$this->db->where ( 'region.id', $id );
		}
		if ($group)
			$this->db->where ( 'groupName', $group );
		if ($search != '')
			$this->db->where ( $search );
		if ($class1 > 0)
			$this->db->where ( 'business.class1', $class1 );
		if ($class2 > 0)
			$this->db->where ( 'business.class2', $class2 );
		$this->db->group_by ( "business.region" );
		$this->db->having ( 'num > 0' );
		$this->db->order_by ( "num", "desc" );
		$query = $this->db->get ( 'region,business' );
		return $query->result_array ();
	}
	
	function getAreaList($id1, $id2, $search, $class1, $class2, $group) {
		if ($id1 > 0) {
			$this->db->select ( 'area.id as id,area.name as name,count(business.id) as num' );
			$this->db->where ( 'area.id = business.area' );
			$this->db->where ( "(state='N' OR state = 'U')" );
			$this->db->where ( 'area.region', $id1 );
			$this->db->where ( 'business.region', $id1 );
			if ($id2 > 0) {
				$this->db->where ( 'area.id', $id2 );
			}
			if ($group)
				$this->db->where ( 'groupName', $group );
			if ($search != '')
				$this->db->where ( $search );
			if ($class1 > 0)
				$this->db->where ( 'business.class1', $class1 );
			if ($class2 > 0)
				$this->db->where ( 'business.class2', $class2 );
			
			$this->db->group_by ( "business.area" );
			$this->db->having ( 'num > 0' );
			$this->db->order_by ( "num", "desc" );
			$query = $this->db->get ( 'area,business' );
			return $query->result_array ();
		} else
			return NULL;
	}
	
	function getBusinessNum($where, $search, $group) {
		if ($group) {
			$this->db->where ( "(state='N' OR state = 'U')" );
			$this->db->where ( 'groupName', $group );
			if ($where)
				$this->db->where ( $where );
			if ($search != '')
				$this->db->where ( $search );
			$query = $this->db->get ( 'business' );
			return $query->num_rows ();
		} else {
			$this->db->where ( "(state='N' OR state = 'U')" );
			if ($where)
				$this->db->where ( $where );
			if ($search != '')
				$this->db->where ( $search );
			$this->db->group_by ( "groupName" );
			$query = $this->db->get ( 'business' );
			return $query->num_rows ();
		}
	}
	
	function checkGroup($name) {
		$this->db->where ( 'groupName', $name );
		if ($this->db->count_all_results ( 'business' ) > 1)
			return TRUE;
		else
			return FALSE;
	}
	
	function getClass1ListforHome() {
		$query = $this->db->get ( 'class1' );
		return $query->result_array ();
	}
	
	function getClass2ListforHome() {
		$query = $this->db->get ( 'class2' );
		return $query->result_array ();
	}
	
	function getRegionListforHome() {
		$this->db->select ( 'region.id as id,region.name as name,COUNT(region.id) as rows' );
		$this->db->where ( 'business.region = region.id' );
		$this->db->group_by ( "id" );
		$this->db->order_by ( "rows", "desc" );
		$query = $this->db->get ( 'business,region' );
		return $query->result_array ();
	}
	
	function getAreaListforHome() {
		$this->db->select ( 'area.id as id,area.name as name,area.region as region,COUNT(area.id) as rows' );
		$this->db->where ( 'business.area = area.id' );
		$this->db->group_by ( "id" );
		$this->db->order_by ( "rows", "desc" );
		$query = $this->db->get ( 'business,area' );
		return $query->result_array ();
	}
	
	function getBusinessImageListforShow($num) {
		$this->db->select ( 'id,logoImage,groupName' );
		$this->db->where ( 'show', 'T' );
		$this->db->where ( "(state='U' OR state='N')" );
		$this->db->group_by ( "groupName" );
		$this->db->limit ( $num );
		$query = $this->db->get ( 'business' );
		return $query->result_array ();
	}
	
	function getAreaListAll2() {
		$query = $this->db->get ( 'area' );
		return $query->result_array ();
	}
	
	function getBusinessListforAdmin($data, $page, $num, $search) {
		$this->db->select ( 'business.id as id,business.name as name,groupName,address,phone,class1.name as class1Name,class2,region.name as regionName,area.name as areaName,details,recycle,show,state,updatePerson,updateTime' );
		$this->db->where ( 'business.class1 = class1.id' );
		$this->db->where ( 'business.region = region.id' );
		$this->db->where ( 'business.area = area.id' );
		if ($search)
			$this->db->where ( $search );
		if ($data ['class1'] > 0)
			$this->db->where ( 'business.class1', $data ['class1'] );
		if ($data ['class2'] > 0)
			$this->db->where ( 'business.class2', $data ['class2'] );
		if ($data ['region'] > 0)
			$this->db->where ( 'business.region', $data ['region'] );
		if ($data ['area'] > 0)
			$this->db->where ( 'business.area', $data ['area'] );
		if ($data ['state'] != 'A')
			$this->db->where ( 'business.state', $data ['state'] );
		if ($data ['show'] != 'N')
			$this->db->where ( 'business.show', $data ['show'] );
		if ($data ['recycle'] != 'N')
			$this->db->where ( 'business.recycle', $data ['recycle'] );
		if ($data ['groupName'])
			$this->db->where ( 'business.groupName', $data ['groupName'] );
		$this->db->order_by ( "business.id", "asc" );
		$this->db->limit ( $num, $page );
		$query = $this->db->get ( 'business,class1,region,area' );
		return $query->result_array ();
	}
	
	function getGroupNameListAll($data) {
		$this->db->select ( 'groupName' );
		if ($data ['class1'] > 0)
			$this->db->where ( 'class1', $data ['class1'] );
		if ($data ['class2'] > 0)
			$this->db->where ( 'class2', $data ['class2'] );
		if ($data ['region'] > 0)
			$this->db->where ( 'region', $data ['region'] );
		if ($data ['area'] > 0)
			$this->db->where ( 'area', $data ['area'] );
		if ($data ['state'] != 'A')
			$this->db->where ( 'state', $data ['state'] );
		if ($data ['show'] != 'N')
			$this->db->where ( 'show', $data ['show'] );
		if ($data ['recycle'] != 'N')
			$this->db->where ( 'recycle', $data ['recycle'] );
		if ($data ['groupName'])
			$this->db->where ( 'groupName', $data ['groupName'] );
		$this->db->group_by ( "groupName" );
		$query = $this->db->get ( 'business' );
		return $query->result_array ();
	}
	
	function getBusinessNumforAdmin($data, $search) {
		if ($search)
			$this->db->where ( $search );
		if ($data ['class1'] > 0)
			$this->db->where ( 'business.class1', $data ['class1'] );
		if ($data ['class2'] > 0)
			$this->db->where ( 'business.class2', $data ['class2'] );
		if ($data ['region'] > 0)
			$this->db->where ( 'business.region', $data ['region'] );
		if ($data ['area'] > 0)
			$this->db->where ( 'business.area', $data ['area'] );
		if ($data ['state'] != 'A')
			$this->db->where ( 'business.state', $data ['state'] );
		if ($data ['show'] != 'N')
			$this->db->where ( 'business.show', $data ['show'] );
		if ($data ['recycle'] != 'N')
			$this->db->where ( 'business.recycle', $data ['recycle'] );
		if ($data ['groupName'])
			$this->db->where ( 'business.groupName', $data ['groupName'] );
		return $this->db->count_all_results ( 'business' );
	}
	
	function delBusinessbyId($id) {
		$this->db->where ( 'id', $id );
		$this->db->delete ( 'business' );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getClass2ListForALL() {
		$query = $this->db->get ( 'class2' );
		return $query->result_array ();
	}
	
	function updateLogo($data, $groupName) {
		$this->db->where ( 'groupName', $groupName );
		$this->db->update ( 'business', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getGroupLogoList($page, $num, $search) {
		$this->db->select ( 'id,groupName,logoImage,show' );
		if ($search) {
			$this->db->where ( $search );
		}
		$this->db->group_by ( "groupName" );
		$this->db->order_by ( "id", "asc" );
		$this->db->limit ( $num, $page );
		$query = $this->db->get ( 'business' );
		return $query->result_array ();
	}
	
	function getGroupLogoNum($search) {
		$this->db->select ( 'groupName' );
		if ($search) {
			$this->db->where ( $search );
		}
		$this->db->group_by ( "groupName" );
		$query = $this->db->get ( 'business' );
		return $query->num_rows ();
	}
}